package ru.dirty.android;


import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;


public class DataParse {

	private String ImgUrl, header, html, comments, autor, rating;
	private boolean starsFlag;
	private int postID;

	public DataParse()
	{

	}
	
	public DataParse(String url, ArrayList<ImageAndText> dirtyNews) throws IOException
	{
		Document doc;
		Element Img;
		Elements divs;
				doc = Jsoup.connect(url).get();   // problems with 2.1
				//Log.i( "dirty", "download complete");
				divs = doc.select("#js-posts_holder div[class^=post]");
				for (Element div : divs){
					starsFlag=false;


					Element feedtype = div.select("div[class=dt feedtype_]").first();

					Element ddClass = div.select("div[class=dd]").first();
						if(feedtype!=null){
							
								
						Img = feedtype.select("img").first();
						header = feedtype.getElementsByTag("h3").text();
			            SpannableString headerContent = new SpannableString(header);
			            headerContent.setSpan(new UnderlineSpan(), 0, headerContent.length(), 0);
						
						Elements miniText = ddClass.getElementsByTag("a");
						Element rate = div.select("div[class=vote]").first();
						comments = miniText.get(2).text();
						
						String buff=ddClass.getElementsByAttribute("href").get(2).attr("href");
						postID=Integer.parseInt(buff.substring(buff.lastIndexOf("/")+1));
						autor = ddClass.text();
						rating = rate.text();

						if(div.html().contains("class=\"stars\""))
						{
							starsFlag=true;
							autor = autor.substring(0,autor.length() - comments.length() - rating.length() - 5);
						}
						else
							autor = autor.substring(0,autor.length() - comments.length() - rating.length() - 4);
						
						html = feedtype.html();
						if(html.contains("<script"))
						{ 
							Log.i("contains","contains");
							String buffer = html;
							html = html.substring(0,html.indexOf("<script"));
							html+= buffer.substring(buffer.lastIndexOf("</script>"),buffer.length());
						}
						Integer index;
						
						if(Img == null)
						{
							if((Img = feedtype.getElementsByClass("post_video").first())!=null)
							{
								index = feedtype.html().lastIndexOf("</h3>");
								html = html.substring(index);
								ImgUrl = Img.attr("style");
								ImgUrl = ImgUrl.substring(ImgUrl.indexOf("http://"),ImgUrl.indexOf(")"));
								//Log.w("comment Img",ImgUrl);
							}
							
							else
								ImgUrl = null;
						}
						
						else
						{
						    index = feedtype.html().lastIndexOf("</p>");
							html = html.substring(index);
							ImgUrl = Img.absUrl("src");
						}
						
						dirtyNews.add(new ImageAndText(ImgUrl,headerContent,html,autor,
				        		comments,rating,starsFlag,postID));
							
					}

				}

		
	}
	
	

	public void parseComments(String url, ArrayList<CommentsItem> data) throws IOException
	{
		Document doc;
		Elements Img;
		Elements divs;
				doc = Jsoup.connect(url).get();
				//Log.i( "dirty", "post download complete");
				divs = doc.select("div[class^=comment indent_]");
				for (Element div : divs){
					Element body = div.select("div[class=c_body]").first();
					Img = body.select("img");

					String comment = body.html();
					String[] imgUrl = {"",""};
					if(Img.first() == null)
						imgUrl=null;
					else
					{
						int count=0;

						Element Image = Img.first();

							for(int i=0;i<Img.size();i++)
							{
								Image = Img.get(i);
								comment = comment.replaceAll(Image.toString(),"");
								comment = comment.replaceAll("<p>","");
								comment = comment.replaceAll("</p>","");
								count++;
							}

							imgUrl[0] = Img.get(0).absUrl("src");

					
					}
					Element elFooter = div.select("div[class=c_footer]").first();
                    String footer = elFooter.text();
                    String aboutUser = elFooter.getElementsByAttribute("href").get(1).attr("href");
                    if(elFooter.html().contains("class=\"dashed\"")){
                        footer = footer.substring(0,footer.length()-16);
                    }
					String rating = div.select("div[class^=vote]").first().text();
					footer = footer.substring(0,footer.length()-rating.length()-3);
					//Log.w("comment:",comment);
					data.add(new CommentsItem(comment,rating,footer,imgUrl,aboutUser));
					}

	}
	
	public String getHtmlText()
	{
		return html;
	}
	
	public String getImageUrl()
	{
		return ImgUrl;
	}
	
	public String getHeader()
	{
		return header;
	}
	
	public String getAutor()
	{
		return autor;
	}
	
	public String getComments()
	{
		return comments;
	}
	
	public String getRating()
	{
		return rating;
	}
	
	public boolean getStarsFlag()
	{
		return starsFlag;
	}
	

	
	
	
}
